﻿using System;
using UnityEngine;
/// <summary>
/// 可同步的数据
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
public class SyncValue :ISyncValue
{
    /// <summary>
    /// 是否为纯数据,指不依赖实体而存在的数据
    /// </summary>
    public bool IsPureValue;

     Type valueType;
    /// <summary>
    /// 所属组件的数据
    /// </summary>
    ComponentData compsData;
    /// <summary>
    /// 此数据在组件中的id
    /// </summary>
    UInt16 valueID;
    protected void __Init(ComponentData comp,UInt16 id)
    {
        compsData = comp;
        valueID = id;
    }
    public object Value
    {
        get
        {
#if UNITY_EDITOR

            //      if (IsPureValue)
#endif
                return compsData.GetValue(valueType,valueID);
            //        return getValue();
        }
        set
        {
            compsData.SetValue(valueID, value);
        }
    }

    public bool IsPure => IsPureValue;

    public object InstanceValue
    {
        get
        {
            if (getValue != null) return getValue();
            else throw new NotImplementedException();




        }
        set {
            if (setValue != null) setValue(value);
            else throw new NotImplementedException();
        }
    }

    public Type ValueType => valueType;

    // public Type ValueType => typeof(T);

    protected Func<object> getValue;
    Action<object> setValue;
    public SyncValue(Type vtype,Action<object>setV)
    {
        valueType = vtype;
        IsPureValue = true;
        setValue = setV;
    }
    public SyncValue(Type vtype,Func<object> getV, Action<object> setV)
    {
        Debug.Assert(ValueUtil.Get(vtype)!=null);
        Debug.Assert(getV!=null && setV!=null);
        valueType = vtype;
        getValue = getV;
        setValue = setV;
        IsPureValue = false;
    }
    public SyncValue(Type vtype,Func<object>getV)
    {
        Debug.Assert(ValueUtil.Get(vtype) != null);
        Debug.Assert(getV != null);
        valueType = vtype;
        getValue = getV;
        setValue = ( v) => Debug.LogWarning("此值不可设置");
        IsPureValue = false;
    }
    public void OnDestroy()
    {
        setValue = null;
        getValue = null;
        compsData = null;
    }
  /*  public void SetValue(object obj)
    {
        Value = (T)obj;
    }

    public object GetValue()
    {
        return Value;
    }

    public object GetInstanceValue()
    {
        return getValue();
    }*/
}

public interface ISyncValue
{

    object Value { get; set; }
    object InstanceValue { get; set; }
 /*   void SetValue(object obj);
    object GetValue();

    object GetInstanceValue();*/
    bool IsPure
    {
        get;
    }
    Type ValueType { get; }
    void OnDestroy();

}

[AttributeUsage(validOn:AttributeTargets.Property,AllowMultiple =false,Inherited =false)]
public class DynamicValueAtrribute : Attribute
{
    public DynamicValueAtrribute() { }
}
[AttributeUsage(validOn: AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
public class PureValueAtrribute : Attribute
{
    public PureValueAtrribute() { }
}